function gaming(){
	//准备时间倒计时
	var num= 3
	var readyPlay = setInterval(function(){
		$(".mask").html("<h2>"+num+"</h2>");
		$("h2").css({
			"text-align":"center",
			"font-size":"100px",
			"line-height":"200px",
			"margin-top":"300px",
			"color":"#fff"
		})
			
		num--;
		if(num<0){
			
			$(".mask").remove()
			clearInterval(readyPlay);
	//将馅饼转进数组
	var arr=["ball","boy","girl","boom","ball","boy","girl"];
	console.log(arr);
	var left = [-30,30,60,90,120,150,180];//馅饼距离屏幕左侧的距离
	
	
	//生成馅饼
	
	var restTime = 30;
	var timer = setInterval(function(){
	 //倒计时
		restTime--;
		$("p").eq(0).html(restTime+"s")
		//生成馅饼
		$(".all").append($("<div class='child'></div>"));
		$(".child").data("canEnter","false");
		
		var xiaobing = arr[Math.floor(Math.random(0,1)*4)];//随机拿到一个馅饼
		var arrLeft = Math.floor(Math.random(0,1)*left.length);//随机产生一个距离左侧的距离
		var scLeft = left[arrLeft]+"px";
		$(".child:last").addClass(xiaobing).css({
			"left":scLeft,
		});
		//给馅饼添加动画
		$(".child:last").animate({"top":"612px"},3000,function(){
			$(this).detach();
		})
		console.log()
		
		if(restTime==0){
			clearInterval(timer);
			clearInterval(posi);
			//游戏结束时的界面
			$(".overPage").animate({"opacity":"1"},2000).css("z-index", "5");
		}
	},1000)
	//获取馅饼的实时位置
	var timing = 0;
	
	//计算获得的分数，并且设置碰撞后的样式
	var score = 0
	var posi = setInterval(function(){
		($(".child").each(function(index,item){
			var res = bump($(".player"), $(item));
			if(res==true){
				if($(item).hasClass("boom")){
					$(".au4")[0].play();
					$(".all").append("<h1 class='over'>you lose</h1>");
					$(".over").css({
						"text-align":"center",
					    "margin": "80px auto",
					    "font-size": "60px",
					    "color": "gold"
					})
					clearInterval(timer);
					clearInterval(posi);
					//游戏结束时的界面
					$(".overPage").animate({"opacity":"1"},2000).css("z-index", "5");
				}else{
					if($(item).hasClass("ball")){
						$(".player").html("<img src='img/Mc_score1.png'>");
						$(".player img").css({
											"position":"absolute",
											"top":"0",
											"left":"0"
										})
						$(".player img").animate({"opacity":"0","top":"-120px"},800)
						score=score+10;
						
					}
					else if($(item).hasClass("girl")||$(item).hasClass("boy")){
						$(".au2")[0].play()
						$(".player").html("<img src='img/Mc_score2.png'>");
						$(".player img").css({
											"position":"absolute",
											"top":"0",
											"left":"0"
									})
						$(".player img").animate({"opacity":"0","top":"-120px"},800)
						score=score+20;
					}
				}
				$(item).detach();
			}
			$("p").eq(1).html(score);
			$(".grade").eq(0).html(score+"  分");
			if(res&&!$("child").data("canEnter")){
				$("child").data("canEnter") = true;
			}
		}));
		if(timing>=30000){
			clearInterval(posi);
		}
	},50)
	//碰撞
	function bump(obj1, obj2) {
		//	console.log(obj2.offsetTop,obj2);
			var t1 = obj1.offset().top + obj1.innerHeight(),
				l1 = obj1.offset().left + obj1.innerWidth(),
				r1 = obj1.offset().left+80,
				b1 = obj1.offset().top+80;
	
				
			var t2 = obj2.offset().top+150,
				l2 = obj2.offset().left+80,
				r2 = obj2.offset().left + obj2.innerWidth(),
				b2 = obj2.offset().top + obj2.innerHeight();
			if(t1 < t2 || l1 < l2 || r1 > r2 || b1 > b2) {
	//			console.log("没有碰到！");
				return false;
			} else {
				console.log("碰到了！");
				$(this).detach();
				return true;
			}
		}
	
	
	
		//触摸位置改变时，的事件
		$(".player").on("touchmove",function(e){
			var x =  e.originalEvent.changedTouches["0"].clientX;//.clientX,触摸的位置
		//判断边界
			if(x>=35 && x<=310){
				$(".player").css("left",x-70+"px");
				console.log(e);
				
			}
			
		})
		}
	},1000)
}
gaming();
//再来一次
$(".Btn_again").on("click",function(){
	 window.location.href="game.html";
	
})
$(".Btn_share").on("click",function(){
	 window.location.href="share.html";
	
})
$(".Btn_rank2").on("click",function(){
	 $(".overPage_share").css("display","block");
	
})



